package com.hot12345.database;

import java.util.List;

import com.hot12345.server.cache.Cache;
import com.hot12345.server.cache.TianyaCacheManager;

 

/**多线程数据库操作
 * 获取数据库数据线程并更新cache中对应数据类�?
 * 获取数据库数据后,更新cache中数据，如果获取不成功则取消更新cache
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class GetDataThread extends Thread {
    private String dbID;
    private String sql;
    private Object key;
    private long lifeTimestamp;
    private Object[] params;

    public GetDataThread(String dbID, String sql,Object[] params, Object key,
                         long lifeTimestamp) {
        this.dbID = dbID;
        this.sql = sql;
        this.key = key;
        this.lifeTimestamp = lifeTimestamp;
        this.params=params;
    }

    /**
     *  
     */
    public void run() {
 
        DBDataProvider dBDataProvider = new DBDataProvider();
        Cache cache = TianyaCacheManager.getCache(dbID);
        try {
            //从数据库中读�?
            List list = dBDataProvider.getMapFromDB(dbID, sql,params);
            //如果读取不成功，取消更新cache
            if (list == null || list.size() <= 0) {
                cache.cancelUpdate(key);
            } else {
                //如果成功，更新cache
                cache.add(key, list, lifeTimestamp);
            }

        } catch (Exception ex) {
            cache.cancelUpdate(key);
        }

    }

}
